package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitivePageReqDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.SensitiveMapper;
import com.heima.wemedia.service.SensitiveService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * Author: Administrator
 * CreateTime: 2024/10/9
 * Project: heima-leadnews
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, WmSensitive> implements SensitiveService {

    @Override
    public ResponseResult list(SensitivePageReqDto dto) {
        // 1.检查参数
        dto.checkParam();

        // 2.分页查询
        IPage<WmSensitive> page = new Page<>(dto.getPage(), dto.getSize());
        page = page(page);

        // 3.返回结果
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        return pageResponseResult;
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult del(Integer id) {
        WmSensitive wmSensitive = getById(id);
        if (wmSensitive == null) return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 保存敏感词
     * @param wmSensitive
     * @return
     */
    public ResponseResult add(WmSensitive wmSensitive) {
        // 1.验证参数
        if (wmSensitive == null) return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        // 2.保存敏感词
        wmSensitive.setCreatedTime(new Date());
        save(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改敏感词
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult update(WmSensitive wmSensitive) {
        if (StringUtils.isBlank(wmSensitive.getSensitives())) return ResponseResult.errorResult(501, "需要填写违禁词！");
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
